<?php
class CategoryControl extends AuthControl{
	//显示栏目
	public function index(){
		$categoryData = cache('category');
		$data = Data::tree($categoryData,'cname');
		$this->assign('data',$data);
		$this->display();
	}
	// public function channelLevel($category,$pid=0,$fieldCid='cid',$fieldPid='pid'){
	// 	$data =array();
	// 	foreach($category as $cat){
	// 		if($cat['pid']==$pid){
	// 			$cat['data']=$this->channelLevel($category,$cat['cid']);
	// 			$data[]=$cat;
	// 		}
	// 	}
	// 	return $data;
	// }
	//添加栏目
	public function add(){
		
		if(IS_POST){
			// p($_POST);exit;
			$Model = K('Category');
			if($Model->addCategory()){
				$this->success('添加成功..',U('index'));
			}else{
				$this->error($Model->error);
			}
		}else{
			$goodsType=cache('goodstype');
			// p($goodsType);exit;
			$this->assign('goodstype',$goodsType);
			$this->display();
		}
	}
	//修改
	public function edit(){
		// p($_SESSION);exit;
		if(IS_POST){ 
			$Model = K('Category');
			if($Model->editCategory()){
				$this->success('修改成功..',U('index'));
			}else{
				$this->error($Model->error);
			}
		}else{
			$cid = Q('cid',0,'intval');
			if($cid){
				$category = cache('category');
				//将自身与子栏目禁用选择
				foreach ($category as $key=>$cat) {
					$disabled="";
					if($cat['cid']==$category[$cid]['pid']){
						$disabled='selected=""';
					}
					if($cat['cid']==$cid){ 
						$disabled='disabled=""';
					}
					//子栏目
					if(Data::isChild($category,$cat['cid'],$cid)){
						$disabled='disabled=""';
					}
					$category[$key]['disabled']=$disabled;
					// $category[$key]['selected']=$selected;
				}
				$this->assign('field',$category[$cid]);
				$this->assign('category',$category);
				$this->display();
			}
		}
	}
	//更新缓存
	public function updateCache(){
		$Model =K("Category");
		$Model->updateCache();
		$this->success('更新成功 ...');
	}
	//删除栏目
	public function del(){
		$Model=K('Category');
		if($Model->d()){
			$this->success('删除成功');
		}else{
			$this->error($Model->error); 
		}

	}



}